package cn.inbot.shopadmin.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.ConstraintLayout;

import com.scwang.smartrefresh.layout.api.RefreshHeader;
import com.scwang.smartrefresh.layout.api.RefreshKernel;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.RefreshState;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;

import cn.inbot.shopadmin.R;

/**
 * Created by shisenkun on 2021/5/10.
 */
public class RefreshHeaderLayout extends ConstraintLayout implements RefreshHeader {

    private RefreshAnimView loadingView;
    private TextView tvMessage;
    private RefreshKernel mRefreshKernel;
    private int mFinishDuration = 500;
    private int mBackgroundColor = R.color.colorPrimary;

    public RefreshHeaderLayout(Context context) {
        this(context, null);
    }

    public RefreshHeaderLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        initRefreshHeaderLayout(context, attrs);
    }

    private void initRefreshHeaderLayout(Context context, AttributeSet attrs) {
        View.inflate(context, R.layout.layout_refresh_header, this);
        loadingView = findViewById(R.id.loadingView);
        tvMessage = findViewById(R.id.tvMessage);
    }

    @NonNull
    @Override
    public View getView() {
        return this;
    }

    @NonNull
    @Override
    public SpinnerStyle getSpinnerStyle() {
        return SpinnerStyle.Translate;
    }

    @SuppressLint("ResourceAsColor")
    @Override
    public void setPrimaryColors(int... colors) {
        if (colors.length > 0) {
            mBackgroundColor = colors[0];
            if(mRefreshKernel != null) {
                mRefreshKernel.requestDrawBackgroundFor(this, mBackgroundColor);
            }
            setBackgroundColor(mBackgroundColor);
        }
    }

    @SuppressLint("ResourceAsColor")
    @Override
    public void onInitialized(@NonNull RefreshKernel kernel, int height, int maxDragHeight) {
        mRefreshKernel = kernel;
        mRefreshKernel.requestDrawBackgroundFor(this, mBackgroundColor);
        setBackgroundColor(mBackgroundColor);
    }

    @Override
    public void onMoving(boolean isDragging, float percent, int offset, int height, int maxDragHeight) {

    }

    @Override
    public void onReleased(@NonNull RefreshLayout refreshLayout, int height, int maxDragHeight) {

    }

    @Override
    public void onStartAnimator(@NonNull RefreshLayout refreshLayout, int height, int maxDragHeight) {

    }

    @Override
    public int onFinish(@NonNull RefreshLayout refreshLayout, boolean success) {
        if (success) {
            tvMessage.setText("刷新成功");
        } else {
            tvMessage.setText("刷新失败");
        }
        return mFinishDuration;  //延迟500毫秒之后再弹回
    }

    @Override
    public void onHorizontalDrag(float percentX, int offsetX, int offsetMax) {

    }

    @Override
    public boolean isSupportHorizontalDrag() {
        return false;
    }

    @Override
    public void onStateChanged(@NonNull RefreshLayout refreshLayout, @NonNull RefreshState oldState, @NonNull RefreshState newState) {
        switch (newState) {
            case None : {
                loadingView.hide();
                break;
            }
            case PullDownToRefresh : {
                tvMessage.setText("下拉刷新");
                loadingView.show();
                break;
            }
            case ReleaseToRefresh : {
                tvMessage.setText("释放刷新");
                break;
            }
            case Refreshing : {
                tvMessage.setText("正在刷新...");
                loadingView.startAnim();
                break;
            }
        }
    }
}
